<?php 

class Spot_controller extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	
	function __construct()
	{
	// Call the controller constructor
		parent::__construct();
		$this->load->model('Spot'); 
		$this->load->database();
		$this->load->helper(array('form','url'));
		$this->load->library('form_validation');
	}

	
	public function create_spot_frame()
	{$this->form_validation->set_rules('name', 'Spot Nick Name:', 'required|trim|xss_clean|min_length[3]||max_length[35]');			
		$this->form_validation->set_rules('directions', 'Directions', 'required|trim|xss_clean');	
		$this->form_validation->set_rules('description', 'Description', 'required|trim|xss_clean');
		$this->form_validation->set_rules('location', 'Location', 'required|trim|xss_clean|max_length[16]');
			
		$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
	
		if ($this->form_validation->run() == FALSE) // validation hasn't been passed
		{
			$this->load->view('create_spot_iframe');
		}
		else // passed validation proceed to post success logic
		{
		 	// build array for the model
			
			$form_data = array(
					       	'name' => set_value('name'),
					       	'directions' => set_value('directions'),
							'description' => set_value('description'),
							'rating' => set_value('rating'),
					       	'location' => set_value('location')
						);
					
			// run insert model to write data to db
		
			if ($this->Spot->insert_spot($form_data) == TRUE) // the information has therefore been successfully saved in the db
			{
				redirect('http://scawesomespots.com/CI/index.php/spot_controller/submit');   // or whatever logic needs to occur
			}
			else
			{
			echo 'An error occurred saving your information. Please try again later';
			// Or whatever error handling is necessary
			}
		}
	}

	function create_spot()
	{
	$this->load->view('create_spot_view');	
	}
	
	function display_spot()
	{
		$id = $this->uri->segment(3,0);
		$data['query'] = $this->Spot->get_spot($id);
		//echo $data;
		$this->load->view('spot_iframe', $data);
	}
	
	function submit()
	{
		$this->load->view('success');
		
	}

}	

/* End of file spot_controller.php */
/* Location: ./application/controllers/spot_controller.php */
?>
